package com.kabigon.tiny.proxy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import static cn.hutool.core.util.ClassUtil.getDefaultValue;

/**
 * Mock 服务代理（JDK 动态代理）
 */
public class MockServerProxy implements InvocationHandler {


    private static final Logger log = LoggerFactory.getLogger(MockServerProxy.class);

    /**
     * 获取指定类型分的默认值 默认值规则为：
     *   1、如果为原始类型，返回0
     *   2、非原始类型返回
     * @param proxy the proxy instance that the method was invoked on
     *
     * @param method the {@code Method} instance corresponding to
     * the interface method invoked on the proxy instance.  The declaring
     * class of the {@code Method} object will be the interface that
     * the method was declared in, which may be a superinterface of the
     * proxy interface that the proxy class inherits the method through.
     *
     * @param args an array of objects containing the values of the
     * arguments passed in the method invocation on the proxy instance,
     * or {@code null} if interface method takes no arguments.
     * Arguments of primitive types are wrapped in instances of the
     * appropriate primitive wrapper class, such as
     * {@code java.lang.Integer} or {@code java.lang.Boolean}.
     *
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Class<?> returnType = method.getReturnType();
        log.info("mock invoked method {}", method.getName());
        return getDefaultValue(returnType);
    }
}
